#region Microsoft Public License
/*
 * (c) Copyright Chenjun Wu. http://utablesdk.codeplex.com
 * 
 * This source is subject to the Microsoft Public License (Ms-PL).
 * Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
 * All other rights reserved.
 */
#endregion

﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Controls;
using System.Windows;
using UTable.Input.MultiTouch;

namespace UTable.Input
{
    /// <summary>
    /// I don't know whether it's relevant to net. But useless now.
    /// </summary>
	public class OpticalProvider: Grid, IInputProvider
	{
		public OpticalProvider()
		{
			StartNetClient();
		}

		private void StartNetClient()
		{
			NetClient netClient = new NetClient();
			netClient.OnNetConnectSucceed += new EventHandler(NetClient_OnNetConnectSucceed);
			netClient.OnNetConnectFail += new EventHandler(NetClient_OnNetConnectFail);
			netClient.OnNetConnectAbort += new EventHandler(NetClient_OnNetConnectAbort);
			netClient.OnMultiTouch += new NetClient.MultiTouchEventHandler(netClient_OnMultiTouch);
		}


		#region IMultiTouchProvider Members

        public event InputEventHandler InputTriggered;

        public void Initialize(UTable.Objects.UApplication context)
        {
            // do nothing
        }

		#endregion


		#region fire cross-thread events
		void netClient_OnMultiTouch(MultiTouchEventArgs args)
		{
			this.Dispatcher.BeginInvoke(new NetClient.MultiTouchEventHandler(OnMultiTouch), new object[] { args });
		}

		void NetClient_OnNetConnectSucceed(object sender, EventArgs e)
		{
			this.Dispatcher.BeginInvoke(new EventHandler(OnNetConnectSucceed), new object[] { sender, e });
		}

		void NetClient_OnNetConnectFail(object sender, EventArgs e)
		{
			this.Dispatcher.BeginInvoke(new EventHandler(OnNetConnectFail), new object[] { sender, e });
		}

		void NetClient_OnNetConnectAbort(object sender, EventArgs e)
		{
			this.Dispatcher.BeginInvoke(new EventHandler(OnNetConnectAbort), new object[] { sender, e });
		}
		#endregion

		#region Net Client Events Handler
		void OnNetConnectSucceed(object sender, EventArgs e)
		{
			Console.WriteLine("Connect Succeed!");
		}
		void OnNetConnectFail(object sender, EventArgs e)
		{
			Console.WriteLine("Connect Fail!");
		}
		void OnNetConnectAbort(object sender, EventArgs e)
		{
			Console.WriteLine("Connection Abort!");
		}
		/// <summary>
		/// Trigger the MultiTouch Event
		/// </summary>
		private void OnMultiTouch(MultiTouchEventArgs args)
		{
            if (InputTriggered != null)
			    InputTriggered(args);
		}


		#endregion

	}
}
